/**
 * Created by yangfan on 2016/10/12.
 */
define(['config/appregister',"i18n","business/personMine/services/httpServices","business/services/filter"],function(app,i18n,httpService){
    app.controller('myUploadCtrl', function($scope,$state,$stateParams) {
        $scope.i18n = i18n;
        $scope.leftNav = ["我的上传","我的下载","我的视频","我的问答","我的评论","我的收藏","我的好友",'我的消息'];
        $scope.tableTitle = ["资料标题","资料分类","浏览量","下载","获得(积分)","上传日期","状态","操作"];

       // 切换title
       $scope.isDataChecked = true;
        $scope.isThesisChecked = false;
        $scope.checkTitle = function(type){
            $scope.listParams.keyWord = "";
            $scope.isCurrent = [];
            $scope.isCurrent[0] = "bold";

            $scope.listParams.pageNo = 1;
            $scope.listParams.pageSize = 10;
            $scope.listParams.itemsPerPage = 10;
            $scope.paginationConf.numberOfPages = parseInt($scope.totalItem/$scope.listParams.pageSize);
            if(type == "data"){
                $scope.isDataChecked = true;
                $scope.isThesisChecked = false;
                init(type);
            }else if(type == "thesis"){
                $scope.isThesisChecked = true;
                $scope.isDataChecked = false;
                init(type);
            }
        };
        $scope.jumpToUpload = function(){
            if($scope.isDataChecked){
                $state.go('eng.uploadSelectFile');
            }else if($scope.isThesisChecked){
                $state.go('eng.libraryUpload');
            }
        };
        $scope.goTo = function(state,item){
            if(!angular.isUndefined(item.fileId)){
                $state.go(state,{tcId:item.tcId,id:item.fileId});
            }else if(!angular.isUndefined(item.thesisId)){
                $state.go(state,{tcId:item.tcId,id:item.thesisId});
            }
        };
        $scope.search = function(){
            if( $scope.isDataChecked == true){
                init('data');
            }else{
                init('thesis');
            }
        };

        $scope.isCurrent = [];
        $scope.isCurrent[0] = "bold";

        $scope.listParams = {
            pageNo:1,
            pageSize:5,
            keyWord:""
        };

        function getPage(type){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage: $scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength:5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function () {
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                    init(type);
                }
            };
            $scope.everPageShow = $scope.paginationConf.perPageOptions;
        }

        function init(type){
            if(type == "data"){
                getListFromData();
            }else if(type == "thesis"){
                getListFormLibrary();
            }
        }
        function getListFromData(){//我的上传
            var req ={
                type:"GET",
                uri:"myUploadFile",
                data:{
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize,
                    keyWord:$scope.listParams.keyWord
                }
            };
            httpService.getMyUploadFile(req).success(function(response){

                $scope.$apply(function(){
                    $scope.myUpload = response.data.list;
                    //todo 待分类 国内国际
                    $scope.myUpload.ecoin = response.data.list.downloadCount*response.data.list.price;
			
                    if($scope.listParams.pageNo == 1){
                        $scope.totalItem = response.data.Pagination.totalCount;
                        getPage("data");
                    }
                    if(response.data.list.length == 0){
                    	$scope.noContent = "您暂时没有下载的资料";
                    }
                });
            }).error(function(){
                console.log("访问出错");
            })
        }
        function getListFormLibrary(){

            var req ={
                type:"GET",
                uri:"myUpload",
                data:{
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize,
                    keyWord:$scope.listParams.keyWord
                }
            };
            httpService.getLibraryUploadFile(req).success(function(response){
                $scope.$apply(function(){
                    $scope.myUpload = response.data.list;
                    //todo 待分类 国内国际
                    //todo 待分类 国内国际
                    $scope.myUpload.ecoin = response.data.list.downloadCount*response.data.list.price;
                    if($scope.listParams.pageNo == 1){
                        $scope.totalItem = response.data.Pagination.totalCount;
                        getPage("thesis");
                    }
                    if(response.data.list.length == 0){
                    	$scope.noContent = "您暂时没有下载的论文";
                    }
                });
            }).error(function(){
                console.log("访问出错");
            })
        }
        var type = "data";
        init(type);

        function getPreviewPage(type){
            //改变每页预览条数
            $scope.changeItemsPerPage = function(item ,index){

            }
        }
        $scope.changeItemsPerPage = function(item ,index){
            $scope.isCurrent = [];
            $scope.isCurrent[index] = "bold";

            $scope.listParams.pageNo = 1;
            $scope.listParams.pageSize = item;
            $scope.paginationConf.itemsPerPage = item;
            $scope.paginationConf.numberOfPages = parseInt($scope.totalItem/$scope.listParams.pageSize);
           //getListFormLibrary();
            if( $scope.isDataChecked == true){
                init('data');
            }else{
                init('thesis');
            }

        }
		
		//跳转详情页
		$scope.goDetail = function(item){
            if(item.authStatus != 1){
                alert('您的文件正在审核中！');
                return;
            }
			var state,param;
			console.log(item)
			if(item.thesisId){
				state = 'eng.libraryDetail';
				param = {thesisId:item.thesisId};
			}
			if(item.fileId){
				state = 'eng.techdetail';
				param = {fileId:item.fileId};
			}
			$state.go(state,param)
		}

    })
});
